//
//  CLrightAndleftViewTextField.m
//  greatControl
//
//  Created by 丁益 on 15/12/19.
//  Copyright © 2015年 dingyi. All rights reserved.
//  输入框的左边和右边都有内容显示

#import "CLrightAndleftViewTextField.h"

#define defaultRightFont  16
#define defaultLeftFont 16

@interface CLRightAndleftViewTextField()

@property (nonatomic, strong) CLUIbutton *rightBtn;

@end

@implementation CLRightAndleftViewTextField


/**
 *  左边显示图标，右边显示文字和图标
 *
 *  @param leftNorImage        左边常规图片
 *  @param leftText            左边文字
 *  @param rightText           右边文字
 *  @param rightNorImage       右边常规图片
 *  @param action              事件方法
 *  @param target              接受者
 */
- (id)initWithFrame:(CGRect)frame leftNorImage:(NSString *)leftNorImage leftText:(NSString *)leftText rightText:(NSString *)rightText rightNorImage:(NSString *)rightNorImage action:(SEL)action target:(id)target{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        // 设置左边显示
        UIButton* leftBtn = [self creatLeftbtnNorImage:leftNorImage title:leftText];
    
        // 设置右边显示
        NSString *rightHighlightImage = [NSString stringWithFormat:@"%@_H",rightNorImage];
        CLUIbutton * btn = [self greatRightBtnNorImage:rightNorImage HighlightImage:rightHighlightImage title:rightText action:(SEL)action target:(id)target];
        _rightBtn = btn;
        
        self.rightView = btn;
        self.leftView = leftBtn;
        
        // 设置右边的view永远显示
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    return self;

}

/**
 * 创建左边的UIButton
 */
- (UIButton*)creatLeftbtnNorImage:(NSString *) leftNorImage  title:(NSString*) leftText{

    UIButton *leftBtn = [[UIButton alloc]init];
    [leftBtn setImage:[UIImage imageNamed:leftNorImage] forState:UIControlStateNormal];
    [leftBtn setTitle: leftText forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:self.leftFont];
    leftBtn.frame = CGRectMake(0, 0,[self getleftTitleW:leftText]+23, self.height);
//    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 2);
    return leftBtn;
}

/**
 *  创建右边的UIButton
 */
- (CLUIbutton*)greatRightBtnNorImage:(NSString *) norImage HighlightImage:(NSString *)HighlightImage title:(NSString*) title action:(SEL)action target:(id)target{
    
    UIImage *btnImage = [UIImage imageNamed:norImage];
    UIImage *btnImage_H = [UIImage imageNamed:HighlightImage];
    if(title == nil) title = @" ";
    CLUIbutton *btn = [[CLUIbutton alloc]initImageW:25 titleW:[self getRightTitleW:title] hight:self.height];
    [btn setImage: btnImage forState:UIControlStateNormal];
    if (btnImage_H) {
        [btn setImage:btnImage_H forState:UIControlStateHighlighted];
    }
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    btn.titleLabel.font = [UIFont systemFontOfSize:self.rightFont];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

/**
 *  右边的文字尺寸
 */
- (CGFloat) rightFont{
    
    if (_rightFont <= 0) {
        _rightFont = defaultRightFont;
    }
    return _rightFont;
}

/**
 *  左边的文字尺寸
 */
- (CGFloat) leftFont{
    
    if (_leftFont <= 0) {
        _leftFont = defaultLeftFont;
    }
    return _leftFont;
}

/**
 *  计算右边文字的宽度
 */
- (CGFloat) getRightTitleW:(NSString*) rightTitle{
    
    CGFloat TitleW = 0;
    TitleW = [rightTitle sizeWithMyFont:[UIFont systemFontOfSize:self.rightFont]].width;
    return TitleW;
}

/**
 *  计算右边文字的宽度
 */
- (CGFloat) getleftTitleW:(NSString*) leftTitle{
    
    CGFloat TitleW = 0;
    TitleW = [leftTitle sizeWithMyFont:[UIFont systemFontOfSize:self.leftFont]].width;
    return TitleW;
}



@end
